﻿using BaseDal;
using Models;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;

namespace BaseBll
{
    public class StoreBll
    {
        static readonly Lazy<StoreBll> bll = new Lazy<StoreBll>(() => new StoreBll());
        public static StoreBll Instance { get; } = bll.Value;
        readonly StoreDal DalInstance;
        private StoreBll() { DalInstance = StoreDal.Instance; }

        /// <summary>
        /// 查询数据是否存在
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public bool IsExist(string strWhere)
        {
            return DalInstance.IsExist(strWhere);
        }

        /// <summary>
        /// 查询list
        /// </summary>
        /// <param name="strWhere"></param>
        /// <returns></returns>
        public List<Store> GetListModels(string strWhere, string filedOrder)
        {
            return DalInstance.GetListModels(strWhere, filedOrder);
        }

        /// <summary>
        /// 将list转为树形json结构
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public JArray GetTreeJson(List<Store> list)
        {
            JArray result = new JArray();
            List<Store> parentList = GetParentNodes(list);
            JArray children;
            JObject parent;

            foreach (Store model in parentList)
            {
                children = GetChilds(list, model.ID);
                parent = new JObject
                {
                    ["Id"] = model.ID,
                    ["StoreName"] = model.StoreName,
                    ["ParentId"] = model.ParentId,
                    ["Status"] = model.Status,
                    ["Description"] = model.Description,
                    ["CreateTime"] = model.CreateTime,
                    ["Head"] = model.Head,
                    ["Phone"] = model.Phone,
                    ["Type"] = model.Type
                };
                if (children.Count > 0)
                {
                    parent["hasChildren"] = true;
                    parent["children"] = children;
                }

                result.Add(parent);
            }
            return result;
        }

        /// <summary>
        /// 获取父节点信息
        /// </summary>
        /// <param name="list"></param>
        /// <returns></returns>
        public List<Store> GetParentNodes(List<Store> list)
        {
            List<Store> result = new List<Store>();
            result.AddRange(list.FindAll(a => a.ParentId == 0));
            return result;
        }

        /// <summary>
        /// 子节点数据
        /// </summary>
        /// <param name="list">列表数据</param>
        /// <param name="id">id</param>
        /// <returns></returns>
        public JArray GetChilds(List<Store> list, int parentId)
        {
            JArray result = new JArray();
            foreach (Store model in list)
            {
                if (model.ParentId == parentId)
                {
                    JObject obj = new JObject();
                    JArray children = GetChilds(list, model.ID);
                    obj["Id"] = model.ID;
                    obj["StoreName"] = model.StoreName;
                    obj["ParentId"] = model.ParentId;
                    obj["Status"] = model.Status;
                    obj["Description"] = model.Description;
                    obj["CreateTime"] = model.CreateTime;
                    obj["Head"] = model.Head;
                    obj["Phone"] = model.Phone;
                    obj["Type"] = model.Type;
                    if (children.Count > 0)
                    {
                        obj["hasChildren"] = true;
                        obj["children"] = children;
                    }
                    result.Add(obj);
                }
            }
            return result;
        }

        /// <summary>
        /// 根据id查询实体类
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Store GetModelById(int id)
        {
            return DalInstance.GetModelById(id);
        }

        /// <summary>
        /// 新增部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int AddStore(Store model)
        {
            return DalInstance.AddStore(model);
        }

        /// <summary>
        /// 修改部门
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int UpdateStore(Store model)
        {
            return DalInstance.UpdateStore(model);
        }

        /// <summary>
        /// 删除仓库
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool DeleteStore(int ID)
        {
            return DalInstance.DeleteStore(ID);
        }

        /// <summary>
        /// 修改状态（启用、禁用）
        /// </summary>
        /// <param name="id"></param>
        /// <param name="userName"></param>
        /// <returns></returns>
        public bool UpdateStatus(int id, int status)
        {
            return DalInstance.UpdateStatus(id, status);
        }

        /// <summary>
        /// 验证model各字段合法性
        /// </summary>
        /// <param name="model"></param>
        /// <param name="msg">错误信息</param>
        /// <returns></returns>
        public bool VerifyModel(Store model, ref string msg)
        {
            if (model.ParentId < 0)
            {
                model.ParentId = 0;
            }

            if (string.IsNullOrEmpty(model.Type))
            {
                msg = "请选择仓库类型";
                return false;
            }

            if (model.Type.Equals("仓库"))
            {
                if (model.ParentId > 0)
                {
                    msg = "仓库不能存在上级，只有库区可以选择上级仓库";
                    return false;
                }
            }

            if (string.IsNullOrEmpty(model.StoreName))
            {
                msg = "仓库名称不能为空";
                return false;
            }
            string strWhere = "";
            if (model.ID > 0)
            {
                // 仓库名称不能重复（库区可以）
                if (model.ParentId == 0)
                {
                    strWhere = " and StoreName='" + model.StoreName + "' and ID !=" + model.ID;
                }

            }
            else
            {
                // 仓库名称不能重复（库区可以）
                if (model.ParentId == 0)
                {
                    strWhere = " and StoreName='" + model.StoreName + "'";
                }

            }
            if (!string.IsNullOrEmpty(strWhere))
            {
                if (IsExist(strWhere))
                {
                    msg = "部门名称已存在";
                    return false;
                }
            }

            return true;
        }

    }
}
